package agend.client.gui.panels.contacts.single;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import myContacts.Email;
import myContacts.PhoneNumber;
import agend.client.gui.utils.HandlerView;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PanelEditEmailPhone extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox<String> comboLabel;
	private JLabel labeIconMail;
	private JLabel labelIconRemove;
	private JLabel labelIconAdd;
	private JTextField fieldLabelValue;

	private Email email;
	private PhoneNumber phoneNumber;

	private boolean save;

	public PanelEditEmailPhone() {
		super();
		initGUI();
		save = false;
		addListener();
	

	}


	private void addListener (){

		
		labelIconAdd.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				if( email!=null )
				{
					String selected = (String)comboLabel.getSelectedItem();

					int i=0;
					while(i<SingleContactPanel.REL_EMAIL_LABEL.length && !SingleContactPanel.REL_EMAIL_LABEL[i].matches(selected))
					{ i++; }

					if(i<SingleContactPanel.REL_EMAIL_LABEL.length)
					{
						email.setRel(SingleContactPanel.REL_EMAIL[i]);
						email.setLabel(null);
					}
					else
					{
						email.setLabel(selected);
						email.setRel(null);
					}

					email.setAddress(fieldLabelValue.getText());
					HandlerView.getBroker().notifyObserversAt(HandlerView.getMenupanel().getContactsPanel().getSingleContactPanel(),email);

				}
				else
				{
					String selected = (String)comboLabel.getSelectedItem();

					int i=0;
					while(i<SingleContactPanel.REL_PHONE_LABEL.length && !SingleContactPanel.REL_PHONE_LABEL[i].matches(selected))
					{ i++; }

					if(i<SingleContactPanel.REL_PHONE_LABEL.length && i!=6)
					{
						phoneNumber.setRel(SingleContactPanel.REL_PHONE[i]);
						phoneNumber.setLabel(null);
					}
					else if(i==6)
					{
						phoneNumber.setLabel(SingleContactPanel.REL_PHONE[6]);
						phoneNumber.setRel(null);
					}
					else
					{
						phoneNumber.setLabel(selected);
						phoneNumber.setRel(null);
					}
					
					phoneNumber.setPhoneNumber(fieldLabelValue.getText());
					HandlerView.getBroker().notifyObserversAt(HandlerView.getMenupanel().getContactsPanel().getSingleContactPanel(),phoneNumber);

				}

				save=true;
				
				

					

			}

			@Override
			
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseEntered(e);
				setCursor(new Cursor(Cursor.HAND_CURSOR));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseExited(e);
				setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}	
		
			
		
		
		});
		
		labelIconRemove.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				if(email != null)
					HandlerView.getBroker().notifyObserversAt(HandlerView.getMenupanel().getContactsPanel().getSingleContactPanel(),"Stop edit table emails");
				else
					HandlerView.getBroker().notifyObserversAt(HandlerView.getMenupanel().getContactsPanel().getSingleContactPanel(),"Stop edit table phones");

			}
			
		});
	

	}

	public boolean isSave() {
		return save;
	}

	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.rowWeights = new double[] {0.1};
				thisLayout.rowHeights = new int[] {7};
				thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1, 0.1};
				thisLayout.columnWidths = new int[] {162, 34, 261, 7, 7};
				this.setPreferredSize(new java.awt.Dimension(517, 31));
				this.setLayout(thisLayout);
				{
					comboLabel = new JComboBox<String>();
					this.add(comboLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					comboLabel.setEditable(true);
				}
				{
					fieldLabelValue = new JTextField();
					this.add(fieldLabelValue, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					labeIconMail = new JLabel();
					this.add(labeIconMail, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labeIconMail.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/icon_mail.jpg")));
				}
				{
					labelIconAdd = new JLabel();
					this.add(labelIconAdd, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelIconAdd.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/add.png")));
				}
				{
					labelIconRemove = new JLabel();
					this.add(labelIconRemove, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelIconRemove.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/remove.png")));
				}
			}
			this.setSize(303, 162);
			this.setBackground(new java.awt.Color(255,255,255));
			//this.setBackground(new java.awt.Color(192,192,192));
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setEmail(Email email) {
		this.email = email;
		phoneNumber = null;
		setComponentByEmailValues();
	}


	private void setComponentByEmailValues()
	{
		List<String> labelsList = new ArrayList<String>(Arrays.asList(SingleContactPanel.REL_EMAIL_LABEL));

		if(email.getLabel()!=null)
		{
			labelsList.add(0, email.getLabel());
		}
		else
		{
			if( email.getRel()!=null )
			{
				int i=0;
				while(i<SingleContactPanel.REL_EMAIL.length && !email.getRel().matches(SingleContactPanel.REL_EMAIL[i]))
					i++;

				if( i==(SingleContactPanel.REL_EMAIL.length-1) )
					labelsList.add(0, " ");
			}
			else
				labelsList.add(0, " ");
		}

		String[] labels = new String[labelsList.size()];
		labelsList.toArray(labels);

		ComboBoxModel<String> comboLabelModel =	new DefaultComboBoxModel<String>(labels);
		comboLabel.setModel(comboLabelModel);

		if(email.getLabel()!=null)
		{
			comboLabel.setSelectedIndex(0);
		}
		else
		{
			if( email.getRel()!=null )
			{
				int i=0;
				while(i<SingleContactPanel.REL_EMAIL.length && !email.getRel().matches(SingleContactPanel.REL_EMAIL[i]))
					i++;

				if( i<(SingleContactPanel.REL_EMAIL.length-1) )
					comboLabel.setSelectedIndex(i);
				else if( i==(SingleContactPanel.REL_EMAIL.length-1) )
					comboLabel.setSelectedIndex(0);
			}
			else
				comboLabel.setSelectedIndex(0);
		}

		fieldLabelValue.setText(email.getAddress());
	}

	public void setPhoneNumber(PhoneNumber phoneNumber) {
		this.phoneNumber = phoneNumber;
		email = null;
		setComponentByPhoneNumber();
	}

	private void setComponentByPhoneNumber()
	{
		List<String> labelsList = new ArrayList<String>(Arrays.asList(SingleContactPanel.REL_PHONE_LABEL));

		if(phoneNumber.getLabel()!=null)
		{
			if(!phoneNumber.getLabel().matches(SingleContactPanel.REL_PHONE[6]))
				labelsList.add(0, phoneNumber.getLabel());			
		}
		else
		{
			if(phoneNumber.getRel()!=null)
			{
				int i=0;
				while(i<SingleContactPanel.REL_PHONE.length && !phoneNumber.getRel().matches(SingleContactPanel.REL_PHONE[i]))
					i++;

				if( i==(SingleContactPanel.REL_PHONE.length-1) )
					labelsList.add(0, " ");
			}
			else
				labelsList.add(0, " ");
		}

		String[] labels = new String[labelsList.size()];
		labelsList.toArray(labels);

		ComboBoxModel<String> comboLabelModel =	new DefaultComboBoxModel<String>(labels);
		comboLabel.setModel(comboLabelModel);

		if(phoneNumber.getLabel()!=null)
		{
			if(phoneNumber.getLabel().matches(SingleContactPanel.REL_PHONE[6]))
				comboLabel.setSelectedIndex(6);
			else
				comboLabel.setSelectedIndex(0);
		}
		else
		{
			if(phoneNumber.getRel()!=null)
			{
				int i=0;
				while(i<SingleContactPanel.REL_PHONE.length && !phoneNumber.getRel().matches(SingleContactPanel.REL_PHONE[i]))
					i++;

				if( i<(SingleContactPanel.REL_PHONE.length-1) )
					comboLabel.setSelectedIndex(i);
				else if( i==(SingleContactPanel.REL_PHONE.length-1) )
					comboLabel.setSelectedIndex(0);
			}
			else
				comboLabel.setSelectedIndex(0);
		}

		fieldLabelValue.setText(phoneNumber.getPhoneNumber());
	}


	


}
